<template>
  <div class="overflow-hidden">
    <div class="h-14 w-full border-b-2 border-gray-200">
      <div class="float-left w-40">
        <img :src="getSettingbyKey('home_logo')" class="m-2" />
      </div>
      <div class="float-left w-40">
        <el-tag dark class="mt-6 ml-4">二维码编辑器</el-tag>

      </div>
      <div class="float-right p-2">

        <el-button type="primary" v-if="type !== 'm' && type !== '2'" @click="dialogVisible = true">设置</el-button>
        <el-button v-if="type !== 'm'" @click="Saveone()">保存</el-button>
        <div v-else>
          <!-- <el-switch v-model="VER" size="large" active-text="V2" inactive-text="V1" /> -->
          <el-cascader v-model="TemplateValue" :props="{ emitPath: false }" :options="TemplateData"
            @change="handleChange" class="ml-4" />
          <el-button @click="TemplateSaveone()">保存模版内容</el-button>
        </div>

      </div>
    </div>
    <div style="clear: both;"></div>
    <div class="w-full ">
      <el-row>
        <el-col :span="4">
          <div class="m-2 border-2 border-gray-200 h-220 p-2 overflow-auto">
            <tw />
          </div>
        </el-col>
        <el-col :span="12">
          <div class="m-2 border-2 border-gray-200 h-220 p-2 overflow-auto">

            <Console @update:childValue="gettitle" :title="title" :ver="VER" />
          </div>
        </el-col>
        <el-col :span="8">
          <div class="m-2  h-200">
            <div class="device iphone-x">
              <!--    外部轮廓    -->
              <div class="frame">
                <div class="content p-1">

                  <preview v-if="listStore.list" :title="title" />
                </div>
              </div>

              <!--    天线    -->
              <div class="stripe"></div>

              <!--    听筒孔    -->
              <div class="header">
                <div class="sensors"></div>
              </div>

              <!--    按键    -->
              <div class="btns"></div>
              <div class="power"></div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog v-model="dialogVisible" title="二维码配置" width="500" :before-close="handleClose">
      <div>
        <div class="h-xs" style="height: 400px;overflow: auto;">
          <div class="p-4">
            访问限制
          </div>
          <div class="p-4">
            <el-radio-group v-model="radiovalue">
              <el-radio v-for="(item, index) in radiolist" :key="index" :custom-style="{ margin: '8px' }"
                :label="item.name" :name="item.name">
              </el-radio>
            </el-radio-group>
            <div v-show="radiovalue === '输入密码查看'" class="bg-gray-3 p-2">
              <el-input v-model="pass" placeholder="访问密码，4-20位数字访问密码" border="surround"
                custom-style="background-color: #fff;"></el-input>
            </div>
          </div>
          <div class="p-4">
            <el-radio-group v-model="radiovalue2">
              <el-radio v-for="(item, index) in radiolist2" :key="index" :custom-style="{ margin: '8px' }"
                :label="item.name" :name="item.name">
              </el-radio>
            </el-radio-group>
          </div>
          <div v-show="radiovalue2 === '自定义'" class="bg-gray-3 p-2">
            <el-date-picker v-model="dt" type="datetimerange" range-separator="-" start-placeholder="开始时间"
              end-placeholder="结束时间" />

          </div>
        </div>



      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="svaeItem()">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog v-model="dialogVisible2" title="二维码" width="500">
      <div>
        <div class="text-center">
          <img :src="jg.eqUrl" class="w-50 h-50" style="margin: 0 auto;" />
        </div>
        <div class="text-center">
          <span class="p-2" style="border-radius: 12px;">
            {{ jg.eqTitle }}
          </span>
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible2 = false">取消</el-button>

        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted, watch } from 'vue'
import { getSettingbyKey } from '@/utils/index.js';
import { templatecategory_list, tradecategory_list, tempalte_list, template_saveone, saveQrcode, i12_template_one } from '@/api/index.js';
import tw from '@/views/components/tw.vue'
import Console from '@/views/components/Console.vue'
import preview from '@/views/components/preview.vue'
import { useListStore } from '@/stores/list';
import { useRoute, useRouter } from 'vue-router';

const listStore = useListStore();
const showToast = inject("$showToast");
//const list = ref({ mdata: [], fdata: [] });
const title = ref('');
// const VER = ref(false)
const route = useRoute();
const type = route.query.t;
console.log('type', type)
const dialogVisible = ref(false)
const dialogVisible2 = ref(false)
const radiolist = ref([{
  name: '不限制',
  disabled: false,
}, {
  name: '输入密码查看',
  disabled: false,
}])
// uv-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
const radiovalue = ref('不限制')
const radiolist2 = ref([{
  name: '永久有效',
  disabled: false,
}, {
  name: '自定义',
  disabled: false,
}])
// uv-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
const radiovalue2 = ref('永久有效')
const pass = ref('')
const dt = ref('')

const handleChange = (value) => {
  console.log(value)
  i12_template_one({ id: value }).then((res) => {
    listStore.setList([]);
    listStore.list = JSON.parse(res.data.data.onedata.tJson)

  });
}
const clickEven = (val) => {

  list.value.mdata.push(val);
  console.log('list', list.value);
}
const gettitle = (val) => {
  console.log('val', val)
  title.value = val;
  console.log('title', title.value);
}
const end1 = (val) => {
  console.log('end1', val)
  list.value = val;
}
const TemplateValue = ref(0);
const TemplateData = ref([])
function geTemplateData() {
  TemplateData.value.push({ label: '场景', value: 0, children: [] })
  TemplateData.value.push({ label: '行业', value: 0, children: [] })
  templatecategory_list().then(async (res) => {
    console.log('res', res)

    //遍历 res.data.listdata  把 cName 和  id 插入 TemplateData[0]
    res.data.data.listdata.forEach(async item => {
      var children = [];
      await tempalte_list({ tcId: item.id }).then((res3) => {
        console.log('res3', res3)
        res3.data.data.listdata.forEach(item => {
          children.push({ label: item.tName, value: item.id })
        });
      })
      TemplateData.value[0].children.push({ label: item.cName, value: item.id, children: children })
    });


  });
  tradecategory_list().then(async (res2) => {
    console.log('res2', res2)

    //遍历 res.data.listdata  把 cName 和  id 插入 TemplateData[0]
    res2.data.data.listdata.forEach(async item => {
      var children = [];
      await tempalte_list({ tpId: item.id }).then((res3) => {
        console.log('res3', res3)
        res3.data.data.listdata.forEach(item => {
          children.push({ label: item.tName, value: item.id })
        });
      })
      TemplateData.value[1].children.push({ label: item.pName, value: item.id, children: children })
    });

    console.log('TemplateData', TemplateData.value)
  });
}
if (type === 'm') {
  geTemplateData();
}
function TemplateSaveone() {
  var data = {
    id: TemplateValue.value,
    tJson: JSON.stringify(listStore.list)
  }
  template_saveone(data).then((res) => {
    showToast('保存成功!', 'success');
  });
}
const jg = ref({})
function Saveone() {
  const data = {}
  data.id = 0
  data.eqTitle = title.value


  // if (listStore.list.fdata.length === 0) {
  //   data.eqType = 0
  // }
  // else {
  //   data.eqType = 1
  // }
  data.eqType = type
  if (type != 2) {
    data.eqJson = JSON.stringify(listStore.list)
  }
  else {
    data.eqJson = JSON.stringify(listStore.list.card)
  }
  data.eqStatus = 1
  data.eqPass = Object.keys(setting.value).length === 0 ? '' : setting.value.pass
  data.eqLimitedperiod = Object.keys(setting.value).length === 0 ? 0 : setting.value.limitedPeriod
  data.eqLimitedperiodStart = Object.keys(setting.value).length === 0 ? null : `${setting.value.limitedPeriod_start}`
  data.eqLimitedperiodEnd = Object.keys(setting.value).length === 0 ? null : `${setting.value.limitedPeriod_end}`
  data.eqOpenmode = 0
  data.eqtId = 0

  saveQrcode(data).then((res) => {
    console.log('res', res)
    if (res.data.code == 200) {
      showToast('保存成功!', 'success');
      jg.value = res.data.data.codeinfo
      dialogVisible2.value = true;
    }
  })
}
const setting = ref({
  limitedPeriod: 0,
  limitedPeriod_start: '',
  limitedPeriod_end: '',
  pass: '',
});
function svaeItem() {
  console.log('dt', dt)
  setting.value = {
    limitedPeriod: radiovalue2.value === '永久有效' ? 0 : 1,
    limitedPeriod_start: dt.value[0],
    limitedPeriod_end: dt.value[1],
    pass: pass.value,

  }
  console.log('setting.value', setting.value)
  dialogVisible.value = false
}
</script>
<style>
.device *,
.device *::before,
.device *::after {
  box-sizing: border-box;

}

.device {
  position: relative;
  margin: 50px auto;
}

.device .frame {
  z-index: 1;
}

.iphone-x {
  width: 342px;
  height: 694px;
}

.iphone-x .frame {
  width: 342px;
  height: 694px;
  padding: 22px;
  background: #222;
  border-radius: 54px;
  box-shadow: inset 0 0 0 2px #606467, inset 0 0 0 6px #e2e3e4;
}

.iphone-x .content {
  position: relative;
  width: 300px;
  height: 650px;
  background-color: #fff;
  background-image: url('bg.jpg');
  background-position: center center;
  background-size: cover;
  border-radius: 32px;
}

.iphone-x .stripe::after,
.iphone-x .stripe::before {
  position: absolute;
  left: 0;
  z-index: 9;
  width: 100%;
  height: 5px;
  content: '';
  border: solid rgb(51 51 51 / 25%);
  border-width: 0 6px;
}

.iphone-x .stripe::after {
  top: 68px;
}

.iphone-x .stripe::before {
  bottom: 68px;
}

.iphone-x .header {
  position: absolute;
  top: 22px;
  left: 50%;
  z-index: 199;
  width: 164px;
  height: 24px;
  margin-left: -82px;
  background: #222;
  border-bottom-right-radius: 16px;
  border-bottom-left-radius: 16px;
}

.iphone-x .header::after,
.iphone-x .header::before {
  position: absolute;
  top: 0;
  width: 6px;
  height: 6px;
  content: '';
}

.iphone-x .header::after {
  left: -6px;
  background: radial-gradient(circle at bottom left, transparent 0, transparent 75%, #222 75%, #222 100%);
}

.iphone-x .header::before {
  right: -6px;
  background: radial-gradient(circle at bottom right, transparent 0, transparent 75%, #222 75%, #222 100%);
}

.iphone-x .sensors::after,
.iphone-x .sensors::before {
  position: absolute;
  content: '';
}

.iphone-x .sensors::after {
  top: 4px;
  left: 50%;
  width: 40px;
  height: 5px;
  margin-left: -20px;
  background: #444;
  border-radius: 2.5px;
}

.iphone-x .sensors::before {
  top: 1px;
  left: 50%;
  width: 11px;
  height: 11px;
  margin-left: 30px;
  background: #444;
  border-radius: 50%;
}

.iphone-x .btns {
  position: absolute;
  top: 92px;
  left: -2px;
  width: 3px;
  height: 26px;
  background: #606467;
}

.iphone-x .btns::after,
.iphone-x .btns::before {
  position: absolute;
  left: 0;
  width: 3px;
  height: 50px;
  content: '';
  background: #606467;
}

.iphone-x .btns::after {
  top: 48px;
}

.iphone-x .btns::before {
  top: 112px;
}

.iphone-x .power {
  position: absolute;
  top: 160px;
  right: -2px;
  width: 3px;
  height: 80px;
  background: #606467;
}
</style>
<style lang="less">
.title {
  height: 32px;
  font-weight: 600;
  line-height: 32px;
  margin-left: 5px;
  padding-left: 5px;
  position: relative;
  color: #000;
  z-index: 0;
}

.title1 {
  padding-left: 15px;

  &:after {
    content: " ";
    width: 4px;
    height: 20px;
    background: #12a3f5;
    position: absolute;
    left: 0;
    top: 6px;
    border-radius: 2px;
  }
}

.title2 {
  padding-left: 15px;

  &:after {
    content: " ";
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #12a3f5;
    position: absolute;
    left: 0;
    top: 12px;
  }
}


.title3 {
  &:after {
    content: " ";
    position: absolute;
    bottom: 0;
    left: 0;
    position: absolute;
    height: 2px;
    width: 100%;
    background: linear-gradient(to right, #01DFE3, rgba(255, 255, 255, 0));

  }
}


.title4 {
  &:after {
    content: attr(data-text);
    position: absolute;
    display: inline-block;
    bottom: -8px;
    left: 5px;
    opacity: 0.2;
    z-index: 0;
    text-align: right;
    transform: rotateY(30deg);
    transform: scaleY(30deg);

  }
}


.title5 {
  color: #10faf8;
  padding-left: 80px;
  line-height: 22px;
  position: relative;
  border-bottom: 2px solid linear-gradient(to right, #01DFE3, rgba(255, 255, 255, 0));

  &:after {
    content: "";
    position: absolute;
    bottom: 0;
    top: 0;
    left: 18px;
    width: 50px;
    height: 18px;
    transform: skewX(35deg);
    background: linear-gradient(to right,
        #2d83fa,
        #10faf8);
  }

  &:before {
    content: "|||";
    display: inline-block;
    font-weight: 900;
    color: #FFF;
    line-height: 30px;
    font-size: 18px;
    position: absolute;
    position: absolute;
    top: -8px;
    left: 0;
    color: #2d83fa;
    transform: skewX(35deg);
  }
}

.title6 {
  color: #C6D039;
  padding-left: 80px;
  line-height: 22px;
  position: relative;
  border-bottom: 2px solid linear-gradient(to right, #01DFE3, rgba(255, 255, 255, 0));

  &:after {
    content: "";
    position: absolute;
    bottom: 0;
    top: 0;
    left: 18px;
    width: 50px;
    height: 18px;
    transform: skewX(35deg);
    background: linear-gradient(to right,
        #4BF15A,
        #C6D039);
  }

  &:before {
    content: "|||";
    display: inline-block;
    font-weight: 900;
    color: #FFF;
    line-height: 30px;
    font-size: 18px;
    position: absolute;
    position: absolute;
    top: -8px;
    left: 0;
    color: #4BF15A;
    transform: skewX(35deg);
  }
}

.title7 {

  &:after {
    content: "";
    position: absolute;
    bottom: 0;
    top: 0;
    left: 0;
    width: 180px;
    height: 100%;
    opacity: 0.3;
    background: linear-gradient(to right,
        #4BF15A,
        #4bf15900);
  }

  &:before {
    content: " ";
    position: absolute;
    bottom: 0;
    left: 0;
    position: absolute;
    height: 2px;
    width: 100%;
    background: linear-gradient(to right, #4BF15A, rgba(255, 255, 255, 0));
  }
}

.title8 {
  border-radius: 16px;
  border: 1px solid #e8e9fb;
  box-shadow: 0 0 10px #e8e9fb;
  text-align: center;
  width: 160px;
  color: #12a3f5;

  &:before {
    content: "";
    width: 300%;
    height: 2px;
    background: #e8e9fb;
    position: absolute;
    top: 15px;
    left: 100%;
    background: linear-gradient(to right, #e8e9fb, rgba(255, 255, 255, 0));
  }
}

.title9 {
  background: #ECF8FF;
  border-top-left-radius: 5px;
  padding-left: 20px;

  &:before {
    content: "";
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
    width: 4px;
    height: 100%;
    background: #50BFFF;
    position: absolute;
    top: 0;
    left: 0;
  }
}

.title10 {
  background: #FFF6F7;
  border-top-left-radius: 5px;
  padding-left: 20px;

  &:before {
    content: "";
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
    width: 4px;
    height: 100%;
    background: #FE6C6F;
    position: absolute;
    top: 0;
    left: 0;
  }
}



.title11 {
  display: inline-block;
  position: relative;
  width: 150px;
  height: 32px;
  line-height: 32px;
  padding-left: 15px;
  background: #50BFFF;
  left: -8px;
  color: #FFF;

  &:before {
    content: "";
    position: absolute;
    height: 0;
    width: 0;
    border-bottom: 8px solid #4396c5;
    border-left: 8px solid transparent;
    top: -8px;
    left: 0;
  }

  &:after {
    content: "";
    position: absolute;
    height: 0;
    width: 0;
    border-top: 15px solid transparent;
    border-bottom: 15px solid transparent;
    border-left: 8px solid #50BFFF;
    right: -8px;
  }
}

.title12 {
  position: relative;
  width: 160px;
  padding-left: 10px;
  // top: 15px;
  // padding: 8px 10px;
  background: #00B3ED;
  box-shadow: -1px 2px 4px rgba(0, 0, 0, 0.5);
  color: #FFF;
  // height: 40px;

  &:before {
    position: absolute;
    content: "";
    display: block;
    width: 7px;
    height: 100%;
    padding: 0 0 7px;
    top: 0;
    left: -7px;
    background: inherit;
    border-radius: 5px 0 0 5px;
  }

  &:before {
    position: absolute;
    content: "";
    display: block;
    width: 5px;
    height: 5px;
    background: rgba(0, 0, 0, 0.35);
    bottom: -5px;
    left: -5px;
    border-radius: 5px 0 0 5px;
  }

}

.title13 {
  border-left: 2px solid #FE6C6F;

  &:after {
    content: " ";
    position: absolute;
    bottom: 0;
    left: 0;
    position: absolute;
    height: 2px;
    width: 60%;
    background: linear-gradient(to right, #FE6C6F, rgba(255, 255, 255, 0));
  }

  &::before {
    content: " ";
    position: absolute;
    top: 0;
    left: 0;
    position: absolute;
    height: 2px;
    width: 30%;
    background: linear-gradient(to right, #FE6C6F, rgba(255, 255, 255, 0));
  }
}

.title14 {
  border-left: 2px solid #01DFE3;

  &:after {
    content: " ";
    position: absolute;
    bottom: 0;
    left: 0;
    position: absolute;
    height: 2px;
    width: 60%;
    background: linear-gradient(to right, #01DFE3, rgba(255, 255, 255, 0));
  }

  &::before {
    content: " ";
    position: absolute;
    top: 0;
    left: 0;
    position: absolute;
    height: 2px;
    width: 30%;
    background: linear-gradient(to right, #01DFE3, rgba(255, 255, 255, 0));
  }
}

.title15 {
  color: #FFF;
  padding-left: 50px;
  background: linear-gradient(-210deg, transparent 1.5em, #01DFE3 0);

  &::before {
    content: '';
    display: block;
    width: 1.73em;
    height: 3em;
    position: absolute;
    background: linear-gradient(-60deg, #577b98 50%, transparent 0);
    left: -3px;
    top: 0;
    border-bottom-left-radius: inherit;
    transform: translateY(-0.5em) rotate(30deg);
    transform-origin: bottom right;
    box-shadow: .2em .2em .3em -.1em rgba(0, 0, 0, .15);
  }
}

.title16 {
  color: #FFF;
  padding-left: 50px;
  background: linear-gradient(-210deg, transparent 1.5em, #FE6C6F 0);

  &::before {
    content: '';
    display: block;
    width: 1.73em;
    height: 3em;
    position: absolute;
    background: linear-gradient(-60deg, #f18384 50%, transparent 0);
    left: -3px;
    top: 0;
    border-bottom-left-radius: inherit;
    transform: translateY(-0.5em) rotate(30deg);
    transform-origin: bottom right;
    box-shadow: .2em .2em .3em -.1em rgba(0, 0, 0, .15);
  }
}

.title17 {
  &:after {
    content: " ";
    position: absolute;
    bottom: 0;
    left: 0;
    position: absolute;
    height: 100%;
    width: 100%;
    animation: animation3 1s linear infinite;
    background: linear-gradient(135deg, #01DFE3 0.25em, transparent 0.25em) -0.5em 0, linear-gradient(225deg, #01DFE3 0.25em, transparent 0.25em) -0.5em 0, linear-gradient(315deg, rgba(238, 161, 99, 0.25) 0.25em, transparent 0.25em) 0 0, linear-gradient(45deg, rgba(238, 161, 99, 0.25) 0.25em, transparent 0.25em) 0 0;
    background-size: 0.75em 0.75em;
    opacity: 0.3;

  }
}

.title18 {
  &:after {
    content: " ";
    position: absolute;
    bottom: 0;
    left: 0;
    position: absolute;
    height: 100%;
    width: 100%;
    opacity: 0.3;
    animation: animation2 1s linear infinite;
    background: repeating-linear-gradient(45deg, #01DFE3 0, #01DFE3 0.25em, transparent 0.25em, transparent 0.5em);
    background-size: 0.75em 0.75em;
  }
}



.hr-solid {
  border: 0;
  border-top: 1px solid #000;
}

.hr-dotted {
  border: 0;
  border-top: 1px dotted #000;
}

.hr-dotted2 {
  border: 0;
  border-top: 2px dotted #000;
}

.hr-dashed {
  border: 0;
  border-top: 1px dashed #000;
}

.hr-dashed2 {
  border: 0;
  border-top: 2px dashed #000;
}

.hr-dashed-fixed {
  border: 0;
  padding-top: 1px;
  background: repeating-linear-gradient(to right, #a2a9b6 0px, #a2a9b6 4px, transparent 0px, transparent 12px);
}

.hr-double {
  border: 0;
  border-top: 3px double #000;
}

.hr-edge-weak {
  border: 0;
  padding-top: 1px;
  background: linear-gradient(to right, transparent, #d0d0d5, transparent);
}

.hr-twill {
  border: 0;
  padding: 3px;
  background: repeating-linear-gradient(135deg, #a2a9b6 0px, #a2a9b6 1px, transparent 1px, transparent 6px);
}

.hr-wavy {
  border: 0;
  color: #000;
  height: .5em;
  white-space: nowrap;
  letter-spacing: 100vw;
  padding-top: .5em;
}

.hr-wavy::before {
  content: "\2000\2000";
  /* IE浏览器实线代替 */
  text-decoration: overline;
  /* 现代浏览器 */
  text-decoration: overline wavy;
}

.hr-shadow {
  border: 0;
  padding-top: 10px;
  color: #000;
  border-top: 1px solid rgba(0, 0, 0, .1);
  box-shadow: inset 0 10px 10px -10px;
}
</style>
